<?php
/* +----------------------------------------------------------------------
// | 	xaoam.com
// |	author:am
// |	gitee:https://gitee.com/xaoam/amAdmin
// |	email:hi@xaoam.com
// |	
// +----------------------------------------------------------------------*/
namespace app\admin\controller;
use think\Controller;
use think\Db;

class Article extends Base {
    public function initialize(){
        parent::initialize();
		$this->model = new \app\admin\model\ArticleM;
    }
	public function lists($page=1,$limit=10){
		$list = $this->model->lists($limit=10);
		foreach($list->items() as $key=>$row){
			$arr[$key] = $row;
			$arr[$key]['opts'] = getTabOpt(['deail','edit','del'],$this->controller,$row['id']);
		}
		return json(['code'=>0,'data'=>$arr,'page'=>$page,'limit'=>$limit,'count'=>$list->total()]);
	}
	public function index(){
		$this->assign('dataUrl',url('article/lists'));
		return $this->fetch();
	}
	public function cue(){
		$data = request()->param();
		$res = $this->model->cue($data);
		if($res){
			$this->reJson['code'] = 200;
			$this->reJson['msg']= '操作成功!';
		}
		return json($this->reJson);
	}		
	public function add(){
		$data = request()->param();
		if(request()->isPost()){
			/*$rePath = $this->upload('image',true);
				if($rePath['data'] != null){
					$data['image'] = $rePath['data']['src'];
				} */
			$res = $this->model->add($data);
			if($res){
				$this->reJson['msg']='增加成功';
				$this->reJson['code']=200;
			}
			return json($this->reJson);
		}else{
			return $this->fetch();
		}
	}
	public function edit(){
		$data = request()->param();
		if(request()->isPost() && isset($data['id'])){
			/*image->input name,$data['id'] ->file表中->from_id,'article'->from_site*/
			$rePath = $this->uploadFile('thumb',$data['id'],'article_thumb');
				if(isset($rePath['data']['src'])){
					$data['thumb'] = $rePath['data']['src'];
				}
			$res = $this->model->edit($data);
			if($res){
				$this->reJson['msg']='编辑成功';
				$this->reJson['code']=200;
			}
			return json($this->reJson);			
		}else{
			$data = Db::name('article')->where('id',$data['id'])->find();
			$this->assign('selectData',json_encode(getTree($this->getTag())));
			$this->assign('data',$data);
			return $this->fetch();
		}
	}
	public function del(){
		$data = request()->param();
		$res = $this->model->del($data);
		$del_arr = [];
		if($res){
			$res = false;
			$this->reJson['msg']='删除成功';
			$this->reJson['code']=200;
			$del_data = Db::name('files')->where('from_id',$data['id'])->select();
			if($del_data){
				/*删除文章中的图片*/
				foreach($del_data as $key=>$row){
					$del_arr['id'][$key] = $row['id'];
					//print_r($row['path']);
					if($row['path']){
						unlink($_SERVER['DOCUMENT_ROOT'] . $row['path']);
					}
				}
				/*删除记录数据表中*/
				Db::name('files')->delete($del_arr['id']);
			}
		}
		return json($this->reJson);
	}
	/*uploadFile($type='file',$from_id=0,$site='')*/
	/*amdata: {'id':19,'site':'article','type':'file'}*/
	public function upload(){
		$data = request()->param('amdata');
		$data = json_decode($data,true);
		if($data['id']){
			$res = $this->uploadFile($data['type'],$data['id'],$data['site']);
			return json($res);
		}
	}
}
